package net.zhuike.novel.app.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;

import net.zhuike.novel.app.R;
import net.zhuike.novel.app.bean.BookChapterBean;
import net.zhuike.novel.app.bean.BookMixAToc;
import net.zhuike.novel.app.bean.BookSit;
import net.zhuike.novel.app.bean.CollBookBean;
import net.zhuike.novel.app.bean.Recommend;
import net.zhuike.novel.app.bean.local.BookRepository;
import net.zhuike.novel.app.bean.support.BookMark;
import net.zhuike.novel.app.bean.user.MenuModel;
import net.zhuike.novel.app.manager.CollectionsManager;
import net.zhuike.novel.app.manager.EventManager;
import net.zhuike.novel.app.manager.SettingManager;
import net.zhuike.novel.app.service.BookReadService;
import net.zhuike.novel.app.ui.adapter.BookMarkAdapter;
import net.zhuike.novel.app.ui.adapter.TocListAdapter;
import net.zhuike.novel.app.ui.utils.FileUtils;
import net.zhuike.novel.app.ui.utils.RxUtils;
import net.zhuike.novel.app.ui.utils.ToastUtils;
import net.zhuike.novel.app.ui.view.recyclerview.EasyRecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.Bind;
import butterknife.OnClick;
import framework.common.baseui.UeBaseActivity;
import framework.common.zanetwork.core.ResponseCallback;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import okhttp3.Response;
import rx.Observable;
import rx.functions.Action1;

/**
 * Created by admin on 2017/6/24.
 * 阅读目录设置
 */

public class ReadMenuActivity extends UeBaseActivity {
    @Bind(R.id.readMenu)
    LinearLayout readMenu;
    @Bind(R.id.readMark)
    LinearLayout readMark;
    @Bind(R.id.readMenuTx)
    TextView readMenuTx;
    @Bind(R.id.readMarkTx)
    TextView readMarkTx;
    @Bind(R.id.menu_recycler_view)
    ListView easyRecyclerView;
    @Bind(R.id.lvMark)
    ListView lvMark;
    @Bind(R.id.showMark)
    LinearLayout showMark;
    private Context mContext;
    private CollBookBean recommendBooks;
    private String bookId;
    private int currentChapter = 1;
    private List<BookChapterBean> mChapterList = new ArrayList<>();
    private TocListAdapter mTocListAdapter;
    private String type = "1";
    private BookMarkAdapter mMarkAdapter;
    private String select = "1";
    protected CompositeDisposable mDisposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_read_menu);
        mContext = this;
        showGoBackBtn();
        hiddenFooter();

        initDatas();

        getMenuText().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(select.equals("1")){
                    if(mChapterList.size()>0) {
                        easyRecyclerView.setSelection(mChapterList.size()-1);
                    }
                }
            }
        });
    }

    @OnClick(R.id.readMenu)
    public void onClickReadMenu() {
        select = "1";
        easyRecyclerView.setVisibility(View.VISIBLE);
        lvMark.setVisibility(View.GONE);
        readMenu.setBackgroundResource(R.drawable.shape_left_press_button);
        readMenuTx.setTextColor(Color.parseColor("#ffffff"));
        readMark.setBackgroundResource(R.drawable.shape_right_button);
        readMarkTx.setTextColor(Color.parseColor("#1089DC"));
    }

    @OnClick(R.id.readMark)
    public void onClickReadMark() {
        select = "2";
        easyRecyclerView.setVisibility(View.GONE);
        lvMark.setVisibility(View.VISIBLE);
        readMenu.setBackgroundResource(R.drawable.shape_left_button);
        readMenuTx.setTextColor(Color.parseColor("#1089DC"));
        readMark.setBackgroundResource(R.drawable.shape_right_press_button);
        readMarkTx.setTextColor(Color.parseColor("#ffffff"));
        updateMark();
    }

    public void initDatas() {
        recommendBooks = getIntent().getParcelableExtra("recommendBooksBean");
        type = getIntent().getStringExtra("type");
        if (type.equals("2")) {
            showMark.setVisibility(View.VISIBLE);
        } else {
            showMark.setVisibility(View.GONE);
        }
        bookId = recommendBooks.getId();

        setTitleText("<<" + recommendBooks.getName() + ">>");
        getTitleView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BookDetailActivity.startActivity(context, bookId);
            }
        });
        if (mChapterList != null)
            easyRecyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    BookChapterBean bookChapterBeen = mTocListAdapter.getData(i);
                    Intent in = new Intent(ReadMenuActivity.this, ReadActivity.class);
                    in.putExtra("goChapters", "1");
                    in.putExtra("bookId", bookId);
                    in.putExtra("currentChapterLink", bookChapterBeen.getLink());
                    in.putExtra("title", bookChapterBeen.getTitle());
                    in.putExtra("type", select);
                    in.putExtra("currentChapter", i + "");
                    in.putExtra("recommendBooksBean", recommendBooks);
                    if (BookRepository.getInstance().getCollBook(bookId) != null) {
                        in.putExtra("isFromSD", true);
                    } else {
                        in.putExtra("isFromSD", false);
                    }
                    if (type.equals("1")) {
                        startActivity(in);
                    }else{
                        setResult(3,in);
                    }
                    finish();
                }
            });
    }

    private void updateMark() {
        List<BookMark> mMarkList = SettingManager.getInstance().getBookMarks(bookId);
        if (mMarkList != null && mMarkList.size() > 0) {
            Collections.reverse(mMarkList);
            readMarkTx.setText("书签  " + mMarkList.size());
        }
        if(mMarkList!=null) {
            mMarkAdapter = new BookMarkAdapter(ReadMenuActivity.this, mMarkList);
            lvMark.setAdapter(mMarkAdapter);
        }
        lvMark.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                BookMark mark = mMarkAdapter.list.get(position);
                if (mark != null) {
                    if (type.equals("1")) {
                        Intent in = new Intent(ReadMenuActivity.this, ReadActivity.class);
                        in.putExtra("goChapters", "2");
                        in.putExtra("source", mark);
                        in.putExtra("isFromSD", false);
                        startActivity(in);
                    } else {
                        setResult(3, new Intent().putExtra("source", mark).putExtra("type", select));
                        finish();
                    }
                } else {
                    ToastUtils.showSingleToast("书签无效");
                }
            }
        });
    }
    public void revomeMark(int  position){
        BookMark mark = mMarkAdapter.list.get(position);
        SettingManager.getInstance().revoeBookMarks(bookId, mark);
        mMarkAdapter.list.remove(position);
        List<BookMark> mMarkList = SettingManager.getInstance().getBookMarks(bookId);
        if (mMarkList != null && mMarkList.size() > 0) {
            Collections.reverse(mMarkList);
            readMarkTx.setText("书签  " + mMarkList.size());
        }else{
            readMarkTx.setText("书签  ");
        }
        mMarkAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Disposable disposable = BookRepository.getInstance()
                .getBookChaptersInRx(bookId)
                .compose(RxUtils::toSimpleSingle)
                .subscribe(
                        (bookChapterBeen, throwable) -> {
                            if (bookChapterBeen.size() > 0) {
                                mChapterList.addAll(bookChapterBeen);
                                if (mChapterList != null) {
                                    recommendBooks.setBookChapters(mChapterList);
                                    mTocListAdapter = new TocListAdapter(this, mChapterList, bookId, currentChapter);
                                    easyRecyclerView.setAdapter(mTocListAdapter);
                                    readMenuTx.setText("目录  " + mChapterList.size());
                                    if(mChapterList.size()>0) {
                                        getMenuText().setVisibility(View.VISIBLE);
                                    }
                                }
                            }
                        }
                );
        if (mDisposable == null) {
            mDisposable = new CompositeDisposable();
        }
        mDisposable.add(disposable);
        MobclickAgent.onPageStart("书籍目录");
        MobclickAgent.onResume(context);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("书籍目录");
        MobclickAgent.onPause(context);
    }
}
